package com.grf.hadoop.order_grouping;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class OrderBean implements WritableComparable<OrderBean> {

    private int id; //订单编号
    private double price; //订单金额

    public OrderBean() {
        super();
    }

    public OrderBean(int id, double price) {
        this.id = id;
        this.price = price;
    }

    @Override
    public int compareTo(OrderBean o) {

        int res;

        if (this.id > o.getId()) {
            res = 1;
        } else if (this.id < o.getId()) {
            res = -1;
        } else {
            if (this.price > o.getPrice()) {
                res = -1;
            } else if (this.price < o.getPrice()) {
                res = 1;
            } else {
                res = 0;
            }
        }

        return res;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(this.id);
        dataOutput.writeDouble(this.price);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.id = dataInput.readInt();
        this.price = dataInput.readDouble();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return this.id + " " + this.price;
    }
}
